package aop;

import api.QrcodeApi;
import bean.getUserInfo.GetUserInfoResponseBean;
import common.pojo.CommonResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pojo.WxUserInfo;
import service.ChatAopService;
import utils.JsonUtil;
import utils.StringUtils;

import javax.smartcardio.CommandAPDU;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by asus on 2018/12/16.
 */
@Aspect
@Component
public class serverAop {

    @Autowired
    QrcodeApi qrcodeApi;
    @Autowired
    ChatAopService chatAopService;
    private static final Logger LOG = LoggerFactory.getLogger(serverAop.class);
    @Around("execution(* controller.WxUserInfoController.getWxUser(..))")
    public CommonResult addQrcode(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        CommonResult result = (CommonResult) joinPoint.proceed(args);
        if (result.isSuccess() && result.getTransientData() != null) {
            WxUserInfo wxUserInfo = (WxUserInfo) result.getTransientData();
            chatAopService.updateQrcode(wxUserInfo);
        }
        return result;
    }

    @Around("execution(* service.LoginService.login(..))")
    public CommonResult addQrcodeMine(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        CommonResult result = (CommonResult) joinPoint.proceed(args);
        if (result.isSuccess() && result.getTransientData() != null) {
           Map map  =(Map) result.getTransientData();
            LOG.warn(JsonUtil.toJson(map));
            LOG.warn(JsonUtil.toJson(map));
            chatAopService.updateQrcodeMine(map.get("openId").toString(),map.get("headUrl").toString());
            chatAopService.addDistribution(map.get("openId").toString());
        }
        return result;
    }
}
